package com.itheima.zuoye.zuoye2;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException;
import org.slf4j.Logger;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
import java.util.stream.Stream;

public class Test {
    static Scanner sc = new Scanner(System.in);
    static Logger log = org.slf4j.LoggerFactory.getLogger(Test.class);

    public static void main(String[] args) throws FileNotFoundException {
        log.info("开始操作");
        while (true) {
            System.out.println("===歌曲菜单===");
            System.out.println("1.显示所有歌曲");
            System.out.println("2.请输入要复制的歌曲的名称：");
            System.out.println("3.请输入要删除的歌曲的名称：");
            System.out.println("4.退出系统：");
            System.out.println("请输入您的选择:");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:

                    shouw();

                    break;
                case 2:

                    copys();

                    break;
                case 3:

                    deate();
                    break;
                case 4:
                    System.out.println("退出系统");
                    log.info("操作结束");
                    return;
                default:
                    System.out.println("输入错误，请重新输入：");
            }
        }

    }

    public static void shouw() {
        try {
            log.info("开始查询");
            String path = new String("D:\\java-base-ws\\JavaAdvanceProject\\day15_Properties\\src\\com\\itheima\\zuoye\\zuoye2\\songs");
            File file = new File(path);
            File[] files = file.listFiles();
            if (files != null && files.length > 0) {
                for (File f : files) {
                    if (f.isFile()) {
                        System.out.println(f.getName());
                    }
                }
            }
            log.info("查询完毕");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void copys() {
        try {
            log.info("开始复制歌曲");
            System.out.println("请输入要复制的歌曲名称");
            String name = sc.next();
            String path = new String("D:\\java-base-ws\\JavaAdvanceProject\\day15_Properties\\src\\com\\itheima\\zuoye\\zuoye2\\songs");
            File file = new File(path);
            File[] files = file.listFiles();
            if (files != null && files.length > 0) {
                boolean flag = false;
                for (File f : files) {
                    if (f.isFile()) {
                        if (f.getName().equals(name)) {
                            //找到了就复制一份到新的文件夹中
                            FileUtil.copy(f, new File("D:\\java-base-ws\\JavaAdvanceProject\\day15_Properties\\src\\com\\itheima\\zuoye\\zuoye2\\esongs"), true);
                            System.out.println("复制成功");
                        }
                    }
                }
                if (!flag){
                    System.out.println("没有找到该文件");
                }
            }
            log.info("复制完毕");
        } catch (IORuntimeException e) {
            System.out.println("没有找到该文件");
            log.error("复制失败");
            throw new RuntimeException(e);
        }

    }

    public static void deate() {
        try {
            log.info("开始删除文件");
            System.out.println("请输入要删除的歌曲名称");
            String name = sc.next();
            String path = new String("D:\\java-base-ws\\JavaAdvanceProject\\day15_Properties\\src\\com\\itheima\\zuoye\\zuoye2\\songs");
            File file = new File(path);
            File[] files = file.listFiles();
            if (files != null && files.length > 0) {
                boolean isDel = false;
                for (File f : files) {
                    if (f.isFile()) {
                        if (f.getName().equals(name)) {
                            //找到了就删除
                            f.delete();
                            isDel = true;
                            System.out.println("删除成功");
                        }
                    }
                }

                if (!isDel) {
                    System.out.println("没有此文件");
                }

            }
            log.info("结束删除文件");
        } catch (Exception e) {
            System.out.println("删除失败");
            log.error("删除文件失败");
            throw new RuntimeException(e);
        }
    }
}
